//
//  HLStatuseOriginalView.swift
//  HLwWeibo
//
//  Created by hqhxu on 16/3/2.
//  Copyright © 2016年 hqhxu. All rights reserved.
//

import UIKit

import SnapKit

import SDWebImage

/// 写在类的外面那么久可以在别的地方也使用
let speaceMagin:CGFloat = 8.0

class HLStatuseOriginalView: UIView {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 记录底部约束的
    var originViewBottonContrain:Constraint?
    
    var statuses:HLStatuses? {
        didSet {
            //给子控件赋值
            if let urlStr = statuses?.user?.profile_image_url {
                // urlStr! 这个地方的urld我们可以认为已经有  不然就是服务器有问题
                iconView.sd_setImageWithURL(NSURL(string: urlStr), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            
            nameLable.text = statuses?.user?.screen_name
            
            timeLable.text = NSDate.dateWithString((statuses?.created_at)!)?.sinaDateDescrpition()
            
            sourceLable.text = statuses?.source
            contentLable.text = statuses?.text
            memberView.image = statuses?.user?.mbrankImage
            verifiedTypeImage.image = statuses?.user?.verifiedImage
            
            //这个时候就可以判断pickureView到底有没有图片
            originViewBottonContrain?.uninstall()  // 在重新布局约束时清除之前的约束
            if let picture  = statuses?.pic_urls where picture.count > 0{
                pictureView.hidden = false
                // 给图片picktureview的模型数据赋值
                pictureView.statuses = statuses
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originViewBottonContrain = make.bottom.equalTo(self.pictureView.snp_bottom).offset(speaceMagin).constraint
                })
            }else {
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originViewBottonContrain = make.bottom.equalTo(self.contentLable.snp_bottom).offset(speaceMagin).constraint
                })
            }
        }
    }
    
    //测试
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.whiteColor()
        prepareUI()
        
    }
    
    
    
    /// UI相关操作
    private func prepareUI() {
        addSubview(iconView)
        addSubview(nameLable)
        addSubview(memberView)
        addSubview(sourceLable)
        addSubview(timeLable)
        addSubview(verifiedTypeImage)
        addSubview(contentLable)
        addSubview(pictureView)


        
        
        // 添加相应的约束
         iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(snp_leading).offset(speaceMagin)
            make.top.equalTo(snp_top).offset(speaceMagin)
            
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLable.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(speaceMagin)
            make.top.equalTo(snp_top).offset(speaceMagin)
        }
        
        memberView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLable.snp_trailing).offset(speaceMagin)
            make.centerY.equalTo(nameLable)
            make.size.equalTo(CGSize(width: 14, height: 14))
            
        }
        
        timeLable.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(iconView.snp_trailing).offset(speaceMagin)
            
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        sourceLable.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLable.snp_trailing).offset(speaceMagin)
            make.centerY.equalTo(timeLable.snp_centerY)
        }
        
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        contentLable.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconView.snp_bottom).offset(speaceMagin)
            make.leading.equalTo(snp_leading).offset(speaceMagin)
            make.trailing.equalTo(snp_trailing).offset(-speaceMagin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLable.snp_bottom).offset(speaceMagin)
            make.leading.equalTo(self.snp_leading).offset(speaceMagin)
            
            //宽度和高度我们得根据item的个数来计算  先暂时写一个固定值
            make.size.equalTo(CGSize(width: 180, height: 120))
        }
        
        
        //给自己添加约束
        self.snp_makeConstraints { (make) -> Void in
            // 如果自身的类容是固定的话那么在自身的类中不需要给自身添加约束   如果自身约束与子控件有关的话那么久需要给自身的控件添加约束 constraint这个才是约束
            self.originViewBottonContrain = make.bottom.equalTo(pictureView.snp_bottom).offset(speaceMagin).constraint
        }
    }
    
    
    // MARK: - 控件的懒加载
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var nameLable:UILabel = {
        let lable = UILabel(textColor: UIColor.darkGrayColor(), font: 14)
        
        lable.sizeToFit()
        
        return lable
    }()
    
    private lazy var sourceLable:UILabel = {
        let lable = UILabel(textColor: UIColor.lightGrayColor(), font: 11)
        lable.sizeToFit()
        
        return lable
    }()

    private lazy var timeLable:UILabel = {
        let lable = UILabel(textColor: UIColor.orangeColor(), font: 11)
        lable.sizeToFit()
        
        return lable
    }()
    
    private lazy var contentLable:UILabel = {
        let lable = UILabel(textColor: UIColor.darkGrayColor(), font: 15)
        
        lable.sizeToFit()
        lable.numberOfLines = 0
        
        return lable
    }()
    
    /// 会员图标
    private lazy var memberView:UIImageView = UIImageView()
    
    private lazy var verifiedTypeImage:UIImageView = UIImageView()
    
    /// 图片view
    lazy var pictureView:HLPickturesView = HLPickturesView()
    
}
